package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Supplier;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.ISupplierService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//供应商
@Controller
@RequestMapping("supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    @RequiresPermissions("supplier:index")
    @PermissionName("供应商首页")
    @RequestMapping("")
    public String index(){
        supplierService.list();
        return "supplier/list";
    }

    //供应商信息列表/带分页
    @RequiresPermissions("supplier:list")
    @PermissionName("供应商首页")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo){
        return supplierService.query(qo);
    }


    //所有供应商信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        return supplierService.list();
    }


    //供应商添加
    @RequiresPermissions("supplier:add")
    @PermissionName("供应商添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Supplier entity){
        JSONResult result = new JSONResult();
        try {
            supplierService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result ;
    }

    //供应商编辑
    @RequiresPermissions("supplier:edit")
    @PermissionName("供应商编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Supplier entity){
        JSONResult result = new JSONResult();
        try {
            supplierService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result ;
    }

    //供应商删除
    @RequiresPermissions("supplier:delete")
    @PermissionName("供应商删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        JSONResult result = new JSONResult();
        try {
            supplierService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result ;
    }
}
